package windowUnits;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import sqlServer.Menus;

import com.swtdesigner.SwingResourceManager;

import windows.MenuEdit;
import windows.NewArticle;

public class CustomIconRenderer extends DefaultTreeCellRenderer {
	
	ImageIcon defaultIconMenu;
	ImageIcon specialIconMenu;
	
	ImageIcon defaultIconPrimaryWeb;
	ImageIcon defaultIconPrimaryWebExpanded;
	ImageIcon specialIconPrimaryWeb;

	public CustomIconRenderer() {
		defaultIconMenu = SwingResourceManager.getIcon(NewArticle.class, "/icons/menuTreeIcon.gif");
		specialIconMenu = SwingResourceManager.getIcon(NewArticle.class, "/icons/menuChangedTreeIcon.gif");
		defaultIconPrimaryWeb = SwingResourceManager.getIcon(NewArticle.class, "/icons/online.png");
		defaultIconPrimaryWebExpanded = SwingResourceManager.getIcon(NewArticle.class, "/icons/offline.png");
		specialIconPrimaryWeb = SwingResourceManager.getIcon(NewArticle.class, "/icons/options.png");
	}

	public Component getTreeCellRendererComponent(JTree tree,

		Object value,boolean sel,boolean expanded,boolean leaf,
		int row,boolean hasFocus) {
	
		super.getTreeCellRendererComponent(tree, value, sel,
		expanded, leaf, row, hasFocus);
		
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
		Object nodeObj = node.getUserObject();
		// check whatever you need to on the node user object
		if(nodeObj.getClass().getName().equals("sqlServer.Menus")){
			//System.out.println(nodeObj+" row "+node.getParent().getIndex(node));
			if (((Menus)nodeObj).isChanged()) {
				setIcon(specialIconMenu);
			} else {
				setIcon(defaultIconMenu);
			}
		}
		if(nodeObj.getClass().getName().equals("windowUnits.PrimaryWeb")){
			//System.out.println(nodeObj+" row "+node.getParent().getIndex(node));
			if (((PrimaryWeb)nodeObj).isChanged()) {
				setIcon(specialIconPrimaryWeb);
			} else {
				if(expanded)
					setIcon(defaultIconPrimaryWebExpanded);
				else
					setIcon(defaultIconPrimaryWeb);
			}
		}
		
		return this;
	}
	
}
